import * as API_NetworkAuth from '../../../api/networkAuth'
import * as API_Passport from '../../../api/passport'
const app = getApp();
Page({
  data:{
    form:{
      mobile:'',
      code:'',
    },
    initTip: '发送验证码',
    endTip: '重新获取',
    sufTip: '秒后重新获取',
    count_time: 60,
    disabled: false,
    interval:null,
    message: '',
  },
  onLoad(){
    // app.autoLogin()
    // var that = this
    // wx.login({
    //   success: function (res) {
    //     var code = res.code;
    //     wx.setStorageSync('getcode', code)
    //     wx.getUserInfo({
    //       success: function (res) {
    //         // that.globalData.userInfo = res.userInfo
    //         typeof cb == "function" && cb(that.globalData.userInfo)
    //       }
    //     })
    //     // 请求后台获取session_key
    //     wx.request({
    //       url: 'https://apibuyer.oupinguoji.cn/passport/mini-program/sessionKey',
    //       method: "get",
    //       data: {code,number:5} ,
    //       success: function (res) {
    //         wx.setStorageSync('open_id', res.data.openid)
    //         wx.setStorageSync('session_key', res.data.session_key)
    //       }
    //     })
    //   }
    // })
  },
  // 表单双向绑定
  syncMobile(e){this.setData({ "form.mobile": e.detail.value })},
  syncCode(e){this.setData({ "form.code": e.detail.value })},
  // 发送验证码
  sendCodeFn(){
    wx.showLoading({
      mask: true
    })
    return new Promise((resolve, reject) => {
      const {
        mobile
      } = this.data.form
      console.log("mobile", mobile, this);
      if (!/^1[3456789]\d{9}$/.test(mobile)) {
        wx.showToast({
          title: '手机号码格式有误！',
          icon: 'none'
        })
        wx.hideLoading()
        return false
      }
      API_Passport.sendLoginSms(mobile, '0000', wx.getStorageSync('uuid')).then(() => {
        wx.showToast({
          title: '验证码发送成功'
        })
        wx.hideLoading()
        this.startCountDown()
        resolve()
      }).catch(e => {
        wx.hideLoading()
        wx.showToast({ 
          title: e.message, 
          icon: 'none' ,
          duration: 3000 
        })
        reject()
      })
    })
  },
  // 发送后计时,重置
  startCountDown() {
    this.setData({
      disabled: true
    })
    this.data.interval = setInterval(() => {
      if (this.data.count_time > 1) {
        this.data.count_time--
        this.data.message = this.data.count_time + this.data.sufTip
        this.setData({
          count_time: this.data.count_time,
          message: this.data.message
        })
      } else {
        clearInterval(this.data.interval)
        this.setData({
          message: '重新发送',
          count_time: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  // 登陆
  async loginFn(){
    const {
      mobile,
      code
    } = this.data.form
    if (!/^1[3456789]\d{9}$/.test(mobile)) {
      wx.showToast({
        title: '手机号码格式有误！',
        icon: 'none'
      })
      return false
    }
    if (code.length != 4) {
      wx.showToast({
        title: '请输入4位短信验证码',
        icon: 'none'
      })
      return false
    }
    let params = {
      // mobile: this.data.form.mobile,
      // sms_code: this.data.form.code,
      uuid: wx.getStorageSync('uuid'),
    }
    // 存储access_token refresh_token user
// let response = await API_NetworkAuth.loginByMobileConnect(wx.getStorageSync('uuid'), params)    
//     wx.setStorageSync('access_token', response.access_token)
//     wx.setStorageSync('refresh_token', response.refresh_token)
//     wx.setStorageSync('uid', response.uid)
let response=await API_Passport.loginByMobile(this.data.form.mobile,this.data.form.code,params)
    console.log(response)
    wx.setStorageSync('access_token', response.access_token)
    wx.setStorageSync('refresh_token', response.refresh_token)
    wx.setStorageSync('uid', response.uid)    
    // 获取用户信息
    const user = await API_NetworkAuth.getUserInfo()
    wx.setStorageSync('user', user)
    // 获取购物车信息
    // const cart_list = await API_Trade.getCarts()
    // wx.setStorageSync('shoplist', cart_list)
    // //获取可用金额
    // const res = await API_Distribution.getAmount()
    // wx.setStorageSync('can_rebate', res.message)
    // //获取待分润金额
    // const response = await API_Distribution.getFenrun()
    // wx.setStorageSync('daifenrun', response.daifenrun)
    // //获取欧币
    // const oucoin = await API_Member.getPoints()
    // wx.setStorageSync('oucoin', oucoin.consum_point)
    wx.navigateBack({
      delta: 2
    })
  
},

})